package com.travelsky.app.util.ftp;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
/**
 * 
 * 项目名称：travelsky   
 * 类名称：FtpClientUtil   
 * 类描述：   FTP客户端的辅助类
 * 创建人：gchzhou gczhou@travelsky.com 
 * 创建时间：2011-5-20 下午01:09:59   
 * 修改人： gchzhou gczhou@travelsky.com
 * 修改时间：2011-5-20 下午01:09:59   
 * 修改备注：   
 * @version 1.0
 */
public class FtpClientUtil {

	private String ip;

	private int port;

	private String username;

	private String password;

	private String path;

	private FtpClient ftpClient;

	public FtpClientUtil(String ip, int port, String username, String password, String path) {
		this.ip = ip;
		this.port = port;
		this.username = username;
		this.password = password;
		this.path = path;
	}

	public boolean connect() {
		try {
			ftpClient = new FtpClient();
			ftpClient.openServer(ip, port);
			ftpClient.login(username, password);
			if (path!=null&&path.length() != 0) 
				ftpClient.cd(path); //path是ftp服务下主目录的子目录
			else
				 ftpClient.cd("/");
			ftpClient.binary();
			//ftpClient.sendServer("quote pasv");
			return true;
		} catch (Exception e) {
			ftpClient = null;
			return false;
		}
	}
 
	public FtpClient getFtpClient() {
		return ftpClient;
	}

	public void setFtpClient(FtpClient ftpClient) {
		this.ftpClient = ftpClient;
	}

	public boolean cd(String dir) {
		try {
			ftpClient.cd(dir);
			return true;
		} catch (IOException e) {
			return false;
		}
	}

	public long download(String ftpFileName, String localFileName)throws Exception {
		long result = 0;
		TelnetInputStream is = null;
		FileOutputStream os = null;

		try {
			is = ftpClient.get(ftpFileName);

			File output = new File(localFileName);
			os = new FileOutputStream(output);

			byte[] bytes = new byte[1024];
			int c = 0;
			while ((c = is.read(bytes)) != -1) {
				os.write(bytes, 0, c);
				result+= c;
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if (is != null)
				is.close();
			if (os != null)
				os.close();
		}
		return result;
	}

	public List getFileNameList(String ftpDir) {
		List list = new ArrayList();
		try {
			DataInputStream dis = new DataInputStream(ftpClient
					.nameList(ftpDir));
			String filename = "";

			while ((filename = dis.readLine()) != null) {
				list.add(filename);
			}
			dis.close();
		} catch (Exception e) {

		}
		return list;
	}

	public void deleteFile(String ftpFileName) {
		ftpClient.sendServer("DELE " + ftpFileName + "\r\n");
		try
		{
			int reply=ftpClient.readServerResponse();
		}
		catch(Exception e)
		{
			
		}
	}

	public boolean disconnect() {
		try {
			if (ftpClient.serverIsOpen()) {
				ftpClient.sendServer("QUIT\r\n");
				int reply = ftpClient.readServerResponse();
			}
		} catch (Exception e) {
			return false;
		} finally {
			ftpClient = null;
		}
		return true;
	}
	
	public long upload(String localFileName,String remoteFileName) throws Exception
	{
		long result=0;
		FileInputStream is=null;
		TelnetOutputStream os=null;
		try
		{
			File localFile=new File(localFileName);
			is=new FileInputStream(localFile);
			os=ftpClient.put(remoteFileName);
			
			byte[] bytes=new byte[1024];
			int c=0;
			while((c=is.read(bytes))!=-1)
			{
				os.write(bytes,0,c);
				os.flush();
				result+=c;
			}
		}
		catch(Exception e)
		{
			throw e;
		}
		finally
		{
			if(is!=null)
				is.close();
			if(os!=null)
				os.close();
		}
		ftpClient.pwd(); // 没有实际作用的指令，用来避免有时候上传到FTP的文件大小变为0的问题
		return result;
	}
}
